package cn.mesmile.grpc.service;

import cn.mesmile.grpc.demo.Greeting;
import cn.mesmile.grpc.demo.HelloWorldServiceGrpc;
import cn.mesmile.grpc.demo.Person;
import com.google.protobuf.ProtocolStringList;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;

/**
 * @author SuperZheng
 */
@Slf4j
@GrpcService
public class GrpcServerService extends HelloWorldServiceGrpc.HelloWorldServiceImplBase {

    @Override
    public void sayHello(Person request, StreamObserver<Greeting> responseObserver) {
        log.info("grpc接收到的参数 {}", request);

        ProtocolStringList nameListList = request.getNameListList();
        StringBuilder sb = new StringBuilder();
        for (String s : nameListList) {
            sb.append(s);
        }
        String message = "Hello " + request.getFirstName() + " " + request.getLastName()+ " "+ sb.toString() +"!";
        Greeting greeting = Greeting.newBuilder().setMessage(message).build();

        log.info("grpc服务端返回的参数 {}", greeting);
        responseObserver.onNext(greeting);
        responseObserver.onCompleted();
    }

}